package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.DocumentTypeDao;
import po.record.DocumentType;

public class DocumentTypeDaoImpl implements DocumentTypeDao{
	private List<DocumentType> dt;
	
	public DocumentTypeDaoImpl() {
		dt = new LinkedList<>();
	}

	@Override
	public boolean insert(DocumentType t) {
		if(dt.contains(t)) {
			return false;
		}
		return dt.add(t);
	}

	@Override
	public boolean update(DocumentType t) {
		int index = dt.indexOf(t);
		if (index == -1) {
			return false;
		} else {
			dt.set(index, t);
		}
		return true;
	}

	@Override
	public boolean delete(int id) {
		Iterator<DocumentType> dts = dt.iterator();
		while (dts.hasNext()) {
			DocumentType doct = dts.next();
			if (doct.getTypeId() == id) {
				doct.setDeleteFlag(true);
				return true;
			}
		}
		return false;
	}

	@Override
	public DocumentType selectById(int id) {
		Iterator<DocumentType> dts = dt.iterator();
		while (dts.hasNext()) {
			DocumentType doct = dts.next();
			if (doct.getTypeId() == id) {
				return doct;
			}
		}
		return null;
	}

	@Override
	public List<DocumentType> selectAll() {
		List<DocumentType> copy = new LinkedList<>();
		for (DocumentType type : dt) {
			if (type.isDeleteFlag() == false) {
				copy.add(type);
			}
		}
		return copy;
	}
}
